﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    public class GetBackRestartTable
    {
        private static Logger _log = Logger.getLogger( typeof( GetBackRestartTable ).FullName );

        private static GetBackRestartTable _instance;

        private Dictionary<int, L1GetBackRestart> _getbackrestart = new Dictionary<int, L1GetBackRestart>();

        public static GetBackRestartTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new GetBackRestartTable();
            }
            return _instance;
        }

        public GetBackRestartTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM getback_restart";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    L1GetBackRestart gbr = new L1GetBackRestart();
                    int area = SqlUtils.toInt( item["area"] );
                    gbr.setArea( area );
                    gbr.setLocX( SqlUtils.toInt( item["locx"] ) );
                    gbr.setLocY( SqlUtils.toInt( item["locy"] ) );
                    gbr.setMapId( SqlUtils.toShort( item["mapid"] ) );

                    _getbackrestart[area] = gbr;
                }
            }
        }

        //public L1GetBackRestart[] getGetBackRestartTableList()
        //{/*TTOK*/
        //    return _getbackrestart.Values.ToArray();
        //}
        /// <summary>
        /// マップIDのリスタート位置を返す。
        /// </summary>
        /// <param name="mapid"></param>
        /// <returns></returns>
        public L1GetBackRestart getGetBackRestart( short mapid )
        {
            foreach ( L1GetBackRestart gbr in _getbackrestart.Values )
            {
                if ( gbr.getArea() == mapid )
                {
                    return gbr;
                }
            }
            return null;
        }
    }
}
